package com.mxl.mvc.cashorder;

import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.mxl.common.DESUtils;
import com.mxl.common.MD5Util;
import com.mxl.common.StringUtil;

/**
 * 网关支付转向控制器测试页面.
 * 
 * @version 1.0
 */
public final class GatewayTestController extends MultiActionController {
	private static final Logger logger = LoggerFactory
			.getLogger(GatewayTestController.class);

	private String sucView;

	public ModelAndView index(final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		// 卡号卡密加密

		String cardno = StringUtil.isEmpty(request.getParameter("card_no")) ? ""
				: request.getParameter("card_no");
		String cardpwd = StringUtil.isEmpty(request.getParameter("card_pwd")) ? ""
				: request.getParameter("card_pwd");
		if (!StringUtil.isEmpty(cardno)) {
			cardno = DESUtils.encrypt(cardno, request.getParameter("key").trim());
			cardpwd = DESUtils.encrypt(cardpwd, request.getParameter("key").trim());
		}
		System.err.println("test gateway cardno:" + cardno + "------cardpwd:"
				+ cardpwd);
		System.err.println(request.getParameterMap());
		// 加签
		String signO = MessageFormat
				.format("version={0}member_code={1}order_time={2}order_id={3}order_amount={4}card_no={5}card_pwd={6}merchant_key={7}",
						request.getParameter("ver_sion"),
						request.getParameter("member_code"),
						request.getParameter("order_time"),
						request.getParameter("order_id"),
						request.getParameter("order_amount"), cardno, cardpwd,
						request.getParameter("key"));
		System.err.println("test gateway signO:" + signO);
		String res = new String(MD5Util.md5Hex(signO).toLowerCase()) + ";"
				+ cardno + ";" + cardpwd;
		response.getWriter().write(res);
		response.getWriter().flush();
		response.getWriter().close();
		return null;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView back(final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		// 卡号卡密加密
		return new ModelAndView(sucView);
	}

	public void setSucView(String sucView) {
		this.sucView = sucView;
	}

}
